﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.BLL.StorageForecast;
using StoragePeriod.Model.StorageForecast;

namespace StoragePeriod.Web.StorageForecast.QualityEstimates
{
    public partial class TheFormulaEdit : System.Web.UI.Page
    {
        T_RawMaterialType tRawMaterialType = new T_RawMaterialType();  //指标类型
        T_QualityCalculateMove tQualityCalculateMove = new T_QualityCalculateMove(); //计算公式

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["RawID"] != null && Request.QueryString["QuotaID"] != null)
                {
                    this.LoadData();
                }
            }
        }

        //隐藏所有计算公式
        private void HidePan(int CalculateTypeID)
        {
            this.Pan_1.Visible = (CalculateTypeID == 1);
            this.Pan_2.Visible = (CalculateTypeID == 2);
            this.Pan_3.Visible = (CalculateTypeID == 3);
            this.Pan_4.Visible = (CalculateTypeID == 4);
            this.Pan_5.Visible = false; //该计算公式已经合并到 3 和 4里面去了
            this.Pan_6.Visible = (CalculateTypeID == 6);
            this.Pan_7.Visible = (CalculateTypeID == 7);
            this.Pan_8.Visible = (CalculateTypeID == 8);
            this.Pan_9.Visible = (CalculateTypeID == 9);
        }

        //加载数据
        private void LoadData()
        {
            int RawID = int.Parse(Request.QueryString["RawID"].ToString());
            int QuotaID = int.Parse(Request.QueryString["QuotaID"].ToString());

            E_RawMaterialType eRawMaterialType = tRawMaterialType.GetModel(QuotaID); //获取指标
            this.lab_QuotaName.Text = eRawMaterialType.TypeName;

            E_QualityCalculateMove eQualityCalculateMove = tQualityCalculateMove.GetModelList("RawID=" + RawID + " and QuotaID=" + QuotaID).FirstOrDefault();
            int CalculateTypeID = 1;
            if (eQualityCalculateMove != null)
            {
                #region 公式类型
                CalculateTypeID = (eQualityCalculateMove.CalculateTypeID == null ? 1 : int.Parse(eQualityCalculateMove.CalculateTypeID.ToString()));

                switch (CalculateTypeID)
                {
                    case 1: //Y（品质）=M(+|-)te次方((正|负)N/T+Z) 
                        this.txt_M.Text = eQualityCalculateMove.M.ToString();
                        this.txt_N.Text = eQualityCalculateMove.N.ToString();
                        this.txt_Z.Text = eQualityCalculateMove.Z.ToString();
                        this.drop_IsNegative.SelectedValue = Convert.ToBoolean(eQualityCalculateMove.IsNegative) ? "1" : "0";//是否负数
                        this.drop_Connector.SelectedValue = eQualityCalculateMove.Connector.ToString();//连接符 1：“+” 2：“-” 3：“*” 4：“/”
                        break;
                    case 2: //Y(失重率)=t*exp(M-N/T)
                        this.txt_2_M.Text = eQualityCalculateMove.M.ToString();
                        this.txt_2_N.Text = eQualityCalculateMove.N.ToString();
                        break;
                    case 3://Y(Vc含量)=M-t*exp(N-Z/T)
                        this.txt_3_M.Text = eQualityCalculateMove.M.ToString();
                        this.txt_3_N.Text = eQualityCalculateMove.N.ToString();
                        this.txt_3_Z.Text = eQualityCalculateMove.Z.ToString();
                        this.txt_3_A.Text = eQualityCalculateMove.A.ToString();
                        this.txt_3_B.Text = eQualityCalculateMove.B.ToString();
                        break;
                    case 4: //Y(Vc含量)=exp{M-t*exp(N-Z/T)}
                        this.txt_4_M.Text = eQualityCalculateMove.M.ToString();
                        this.txt_4_N.Text = eQualityCalculateMove.N.ToString();
                        this.txt_4_Z.Text = eQualityCalculateMove.Z.ToString();
                        this.txt_4_A.Text = eQualityCalculateMove.A.ToString();
                        this.txt_4_B.Text = eQualityCalculateMove.B.ToString();
                        break;
                    case 5://Y(Vc下降率)=(A-Y(Vc含量))*100/B    已经合并到 3 和4里面去了
                        this.txt_5_A.Text = eQualityCalculateMove.A.ToString();
                        this.txt_5_B.Text = eQualityCalculateMove.B.ToString();
                        break;
                    case 6://C(失重率)=M*10(N次方)*exp(-Z/T)*t
                        this.txt_6_M.Text = eQualityCalculateMove.M.ToString();
                        this.txt_6_N.Text = eQualityCalculateMove.N.ToString();
                        this.txt_6_Z.Text = eQualityCalculateMove.Z.ToString();
                        break;
                    case 7://Y(主观评分)=10-t*exp(M-N/T)
                        this.txt_7_M.Text = eQualityCalculateMove.M.ToString();
                        this.txt_7_N.Text = eQualityCalculateMove.N.ToString();
                        break;
                    case 8://C(感官评分)=10-M*10(N次方)*exp(-Z/T)*t
                        this.txt_8_M.Text = eQualityCalculateMove.M.ToString();
                        this.txt_8_N.Text = eQualityCalculateMove.N.ToString();
                        this.txt_8_Z.Text = eQualityCalculateMove.Z.ToString();
                        break;
                    case 9://Y（水果Vc含量）=M*exp(-[N*exp(-Z/T)*t])
                        this.txt_9_M.Text = eQualityCalculateMove.M.ToString();
                        this.txt_9_N.Text = eQualityCalculateMove.N.ToString();
                        this.txt_9_Z.Text = eQualityCalculateMove.Z.ToString();
                        this.txt_9_A.Text = eQualityCalculateMove.A.ToString();
                        this.txt_9_B.Text = eQualityCalculateMove.B.ToString();
                        break;
                }
                #endregion
            }
            this.Drop_CalculateType.SelectedValue = CalculateTypeID.ToString();
            this.Drop_CalculateType_SelectedIndexChanged(null, null);
        }

        //提交数据
        protected void btPost_Click(object sender, EventArgs e)
        {
            int RawID = int.Parse(Request.QueryString["RawID"].ToString());
            int QuotaID = int.Parse(Request.QueryString["QuotaID"].ToString());
            bool IsAdd = false;

            E_QualityCalculateMove eQualityCalculateMove = tQualityCalculateMove.GetModelList("RawID=" + RawID + " and QuotaID=" + QuotaID).FirstOrDefault();
            if (eQualityCalculateMove == null)
            {
                eQualityCalculateMove = new E_QualityCalculateMove();
                IsAdd = true;
            }
            eQualityCalculateMove.RawID = RawID;
            eQualityCalculateMove.QuotaID = QuotaID;
            eQualityCalculateMove.CalculateTypeID = int.Parse(this.Drop_CalculateType.SelectedValue);

            #region 公式类型
            switch (eQualityCalculateMove.CalculateTypeID)
            {
                case 1:////Y（品质）=M(+|-)te次方((正|负)N/T+Z) 
                    eQualityCalculateMove.M = decimal.Parse(this.txt_M.Text);
                    eQualityCalculateMove.N = decimal.Parse(this.txt_N.Text);
                    eQualityCalculateMove.Z = decimal.Parse(this.txt_Z.Text);
                    eQualityCalculateMove.IsNegative = (this.drop_IsNegative.SelectedValue == "1");//是否负数
                    eQualityCalculateMove.Connector = int.Parse(this.drop_Connector.SelectedValue);//连接符 1：“+” 2：“-” 3：“*” 4：“/”
                    break;
                case 2:////Y(失重率)=t*exp(M-N/T)
                    eQualityCalculateMove.M = decimal.Parse(this.txt_2_M.Text);
                    eQualityCalculateMove.N = decimal.Parse(this.txt_2_N.Text);
                    break;
                case 3://Y(Vc含量)=M-t*exp(N-Z/T)
                    eQualityCalculateMove.M = decimal.Parse(this.txt_3_M.Text);
                    eQualityCalculateMove.N = decimal.Parse(this.txt_3_N.Text);
                    eQualityCalculateMove.Z = decimal.Parse(this.txt_3_Z.Text);
                    eQualityCalculateMove.A = decimal.Parse(this.txt_3_A.Text);
                    eQualityCalculateMove.B = decimal.Parse(this.txt_3_B.Text);
                    break;
                case 4: //Y(Vc含量)=exp{M-t*exp(N-Z/T)}
                    eQualityCalculateMove.M = decimal.Parse(this.txt_4_M.Text);
                    eQualityCalculateMove.N = decimal.Parse(this.txt_4_N.Text);
                    eQualityCalculateMove.Z = decimal.Parse(this.txt_4_Z.Text);
                    eQualityCalculateMove.A = decimal.Parse(this.txt_4_A.Text);
                    eQualityCalculateMove.B = decimal.Parse(this.txt_4_B.Text);
                    break;
                case 5://Y(Vc下降率)=(A-Y(Vc含量))*100/B  已经合并到 3  和 4里面去了
                    eQualityCalculateMove.A = decimal.Parse(this.txt_5_A.Text);
                    eQualityCalculateMove.B = decimal.Parse(this.txt_5_B.Text);
                    break;
                case 6://C(失重率)=M*10(N次方)*exp(-Z/T)*t
                    eQualityCalculateMove.M = decimal.Parse(this.txt_6_M.Text);
                    eQualityCalculateMove.N = decimal.Parse(this.txt_6_N.Text);
                    eQualityCalculateMove.Z = decimal.Parse(this.txt_6_Z.Text);
                    break;
                case 7://Y(主观评分)=10-t*exp(M-N/T)
                    eQualityCalculateMove.M = decimal.Parse(this.txt_7_M.Text);
                    eQualityCalculateMove.N = decimal.Parse(this.txt_7_N.Text);
                    break;
                case 8://C(感官评分)=10-M*10(N次方)*exp(-Z/T)*t
                    eQualityCalculateMove.M = decimal.Parse(this.txt_8_M.Text);
                    eQualityCalculateMove.N = decimal.Parse(this.txt_8_N.Text);
                    eQualityCalculateMove.Z = decimal.Parse(this.txt_8_Z.Text);
                    break;
                case 9://Y（水果Vc含量）=M*exp(-[N*exp(-Z/T)*t])
                    eQualityCalculateMove.M = decimal.Parse(this.txt_9_M.Text);
                    eQualityCalculateMove.N = decimal.Parse(this.txt_9_N.Text);
                    eQualityCalculateMove.Z = decimal.Parse(this.txt_9_Z.Text);
                    eQualityCalculateMove.A = decimal.Parse(this.txt_9_A.Text);
                    eQualityCalculateMove.B = decimal.Parse(this.txt_9_B.Text);
                    break;
            }
            #endregion

            if (IsAdd)
            {
                tQualityCalculateMove.Add(eQualityCalculateMove);
            }
            else
            {
                tQualityCalculateMove.Update(eQualityCalculateMove);
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('保存成功！');</script>");
        }

        //公式变换
        protected void Drop_CalculateType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int CalculateTypeID = int.Parse(this.Drop_CalculateType.SelectedValue);
            this.HidePan(CalculateTypeID);
        }
    }
}